/**
 * @author:linjiye
 * @date: 2024/01/13
 * @desc: 用于全局接口请求自定义控制字段
 **/

// import {fetchBaseQuery} from "@reduxjs/toolkit/query/react"
// import {useApiPath} from "@/hooks/useApiPath";
// import useGetStorage from "@/hooks/useGetStorage";
//
// let token = null
// useGetStorage('token').then(res => {
//     token = res
// })
//
// const useBaseQuery = (baseUrlModule) => {
//     return async (args, api, extraOptions) => {
//
//         const baseUrl = process.env.EXPO_PUBLIC_API_URL + useApiPath(baseUrlModule)
//         // const baseUrl = 'http://192.168.10.122:8080/' + useApiPath(baseUrlModule)
//         if (token) {
//
//             args.headers = {
//                 ...args.headers,
//                 Authorization: token,
//                 'X-Access-Token': token,
//             }
//         }
//
//         const result = await fetchBaseQuery({baseUrl})(args, api, extraOptions)
//         if (result?.error) result.error.openErrMessage = args.openErrMessage || false
//         if (result?.meta?.response) result.meta.response.openErrMessage = args.openErrMessage || false
//         if (result?.data) result.data.openErrMessage = args.openErrMessage || false
//         return result
//     }
// }
//
// export default useBaseQuery

import { fetchBaseQuery } from "@reduxjs/toolkit/query/react"
import { useApiPath } from "@/hooks/useApiPath"
import useGetStorage from "@/hooks/useGetStorage"

const useBaseQuery = (baseUrlModule) => {
    return async (args, api, extraOptions) => {
        try {
            // 动态获取最新 token
            const token = await useGetStorage('token')

            const baseUrl = process.env.EXPO_PUBLIC_API_URL + useApiPath(baseUrlModule)

            // 克隆 headers 对象避免污染原始参数
            const headers = token ? {
                ...args.headers,
                Authorization: token,
                'X-Access-Token': token,
            } : args.headers

            // 创建带动态 header 的 fetch 实例
            const baseQuery = fetchBaseQuery({
                baseUrl,
                prepareHeaders: (headers) => {
                    if (token) {
                        headers.set('Authorization', token)
                        headers.set('X-Access-Token', token)
                    }
                    return headers
                }
            })

            // 执行请求
            const result = await baseQuery(args, api, extraOptions)

            if (result?.error) result.error.openErrMessage = args.openErrMessage || false
            if (result?.meta?.response) result.meta.response.openErrMessage = args.openErrMessage || false
            if (result?.data) result.data.openErrMessage = args.openErrMessage || false

            return result

        } catch (error) {
            // 处理 token 获取失败的情况
            return {
                error: {
                    status: 'CUSTOM_ERROR',
                    error: 'Token retrieval failed',
                    data: error
                }
            }
        }
    }
}

export default useBaseQuery
